<template>
  <div class="warp-box  clearfix">
    <div class="warp-con">
      <div class="header">
        <div class="headerWrapper">
          <div class="search">
            <el-form :inline="true" class="demo-form-inline">
              <el-form-item>
                <el-input v-model="name" placeholder="请输入部门名称" @keyup.enter.native="query" auto-complete="off"
                  clearable />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="query">查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="add">新增</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <div class="content">
        <el-table :data="tableData" :header-row-style="HeaderStyle" :row-style='cellStyle' border row-key="id"
          v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)" max-height="660">
          <el-table-column align="center" prop="name" label="名称" show-overflow-tooltip />
          <el-table-column align="center" prop="principal" label="负责人" show-overflow-tooltip />
          <el-table-column align="center" prop="principalPhone" label="负责人电话" show-overflow-tooltip />
          <el-table-column align="center" prop="linkman" label="联系人" show-overflow-tooltip />
          <el-table-column align="center" prop="linkmanPhone" label="联系人电话" show-overflow-tooltip />
          <el-table-column align="center" prop="sortNo" label="排序" show-overflow-tooltip />
          <el-table-column align="center" prop="describe" label="备注" show-overflow-tooltip />
          <el-table-column label="操作" align="center" min-width="130">
            <template slot-scope="scope">
              <span v-if="!scope.row.isAudit" @click="showRelevance(scope.row)" class="pointer">物种权限<i class="line"></i></span>
              <span @click="update(scope.row)" class="pointer">修改</span><i class="line"></i>
              <span @click="remove(scope.row)" class="pointer">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="paging">
        <el-pagination :page-sizes="[10]" :page-size="this.pageSize" :current-page="pageIndex" :total="totalCount"
                       @current-change="pager" background layout="total, sizes, prev, pager, next, jumper" />
      </div>
    </div>
    <el-dialog :title="formTitle" :visible.sync="dialogVisible" :before-close="cancel" :close-on-click-modal="false">
      <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input class="input" v-model="dataForm.name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级" prop="parentId">
              <SelectTree :options="tableData" :value="dataForm.parentId" :clearable="true" :accordion="false"
                @getValue="getValue($event)" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="principalId">
              <el-select v-model="dataForm.principalId" @change="handleChange" clearable>
                <el-option v-for="item in userData" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人电话" prop="principalPhone">
              <el-input class="input" v-model="dataForm.principalPhone" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="linkman">
              <el-input class="input" v-model="dataForm.linkman" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人邮箱" prop="linkmanEmail">
              <el-input class="input" v-model="dataForm.linkmanEmail" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="种质权限" prop="isAudit">
              <el-radio-group v-model="dataForm.isAudit">
                <el-radio border :label="true">所有权限</el-radio>
                <el-radio border :label="false">分配权限</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sortNo">
              <el-input-number class="input" v-model="dataForm.sortNo" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="地址" prop="address">
          <el-input class="input" v-model="dataForm.address" />
        </el-form-item>
        <el-form-item label="备注" prop="describe">
          <el-input type="textarea" :autosize="{ minRows: 3}" v-model="dataForm.describe" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save" class="dialogBtn">
          <span v-if="formEdit == 'edit'">修 改</span>
          <span v-else>保 存</span>
        </el-button>
        <el-button type="primary" @click="cancel" class="dialogBtn">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="物种权限" :visible.sync="speciesVisible" :before-close="cancelRelevance" :close-on-click-modal="false">
      <el-transfer v-model="speciesIds" :data="speciesTree" :props="props" :titles="['物种列表', '已选物种']" filterable style="width: 500px;margin: 0 auto;" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveRelevance" class="dialogBtn">
          保存
        </el-button>
        <el-button type="primary" @click="cancelRelevance" class="dialogBtn">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

  import systemApi from '@/api/systemApi'
  import SelectTree from '@/components/TreeSelect'
  export default {
    components: {
      SelectTree
    },
    data() {
      return {
        loading: false,
        name: '',
        cellStyle: {
          height: '55px',
          fontSize: '16px'
        },
        HeaderStyle: {
          height: '60px',
          fontSize: '16px'
        },
        pageIndex: 1,
        pageSize: 10,
        totalCount: 0,
        tableData: [],
        dataForm: {},
        formEdit: 'detail',
        formTitle: null,
        dialogVisible: false,
        rules: {
          name: [{
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }]
        },
        selectedId: null,
        speciesVisible: false,
        speciesIds: [],
        speciesTree: [],
        props: {
          key: 'id',
          label: 'speciesName'
        },
        userData: [],
        userMap: {}
      }
    },
    created() {
      this.findPage()
    },
    methods: {
      query() {
        this.pageIndex = 1;
        this.findPage();
      },
      pager(val) {
        this.pageIndex = val
        this.findPage()
      },
      findPage() {
        this.loading = true
        let params = {
          sort: 'sortNo',
          order: 'ASC',
          name: this.name,
          pageSize: this.pageSize,
          pageIndex: this.pageIndex
        }
        systemApi.findDepartPage(params).then(res => {
          this.loading = false
          let result = res.data.result
          if (res.data.message === 'success' && result) {
            this.tableData = result.rows
            this.totalCount = result.totalCount
          } else {
            this.tableData = []
            this.totalCount = 0
          }
        })
      },
      add() {
        this.formEdit = 'add'
        this.formTitle = '新增部门'
        this.getUser()
        this.dataForm.isAudit = false
        this.dialogVisible = true
      },
      update(row) {
        this.formEdit = 'edit'
        this.formTitle = '修改部门'
        this.getUser()
        systemApi.getDepartmentInfo(row.id).then((res) => {
          if (res.data.message === 'success' && res.data.result) {
            this.dataForm = res.data.result
            this.dialogVisible = true
          } else {
            this.dataForm = {}
          }
        })
      },
      save() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.showLoading(this.formEdit === 'edit' ? '正在修改' : '正在保存')
            systemApi.saveDepartment(this.dataForm).then((res) => {
              this.hideLoading()
              if (res.data.message === 'success') {
                this.cancel()
                this.findPage()
                let message = this.formEdit === 'edit' ? '修改成功！' : '保存成功！'
                this.$notify({
                  title: '消息',
                  message: (message),
                  position: 'bottom-right',
                  showClose: false,
                  type: 'success'
                })
              }
            })
          }
        })
      },
      cancel() {
        this.dataForm = {}
        this.dialogVisible = false
      },
      getValue(value) {
        if (this.dataForm.id === value) {
          this.$message.error('不能选择自己')
        } else {
          this.dataForm.parentId = value
        }
      },
      remove(row) {
        let tips = ''
        if (row) {
          tips = `此操作将删除: ${row.name}, 是否继续?`
        } else {
          return
        }
        this.$confirm(tips, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          systemApi.deleteDeparment(row.id).then((res) => {
            if (res.data.message === 'success') {
              this.findPage()
              this.$notify({
                title: '消息',
                message: ('删除成功！'),
                position: 'bottom-right',
                showClose: false,
                type: 'success'
              })
            }
          })
        }).catch((error) => {
          console.log(error)
        })
      },
      showRelevance(row) {
        this.selectedId = row.id
        this.getRelation(row.id)
        systemApi.findSpeciesList().then(res => {
          this.speciesVisible = true
          if (res.data.message === 'success') {
            this.speciesTree = res.data.result
          } else {
            this.speciesTree = []
          }
        })
      },
      getRelation(param) {
        systemApi.getDepartmentRelation(param).then(res => {
          if (res.data.message === 'success') {
            this.speciesIds = res.data.result
          }
        })
      },
      cancelRelevance() {
        this.selectedId = null
        this.speciesIds = []
        this.speciesTree = []
        this.speciesVisible = false
      },
      saveRelevance() {
        let params = {
          departmentId: this.selectedId,
          speciesIds: this.speciesIds
        }
        this.showLoading('正在保存')
        systemApi.saveDepartmentRelation(params).then(res => {
          this.hideLoading()
          if (res.data.message === 'success') {
            this.cancelRelevance()
            this.$notify({
              title: '消息',
              message: '保存成功',
              position: 'bottom-right',
              showClose: false,
              type: 'success'
            })
          }
        })
      },
      getUser() {
        this.userData = []
        this.userMap = {}
        systemApi.getUserList().then(res => {
          let result = res.data.result
          if (res.data.message === 'success' && result) {
            this.userData = result
            for (let i in result) {
              let item = result[i]
              this.$set(this.userMap, item.id, item)
            }
          }
        })
      },
      handleChange(value) {
        let phone = ''
        let principal = ''
        if (value) {
          let user = this.userMap[value]
          principal = user.name
          phone = user.mobilePhoneNo
        }
        this.$set(this.dataForm, 'principal', principal)
        this.$set(this.dataForm, 'principalPhone', phone)
      },
      showLoading(message) {
        this.$loading({
          lock: true,
          text: message,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      },
      hideLoading() {
        this.$loading().close()
      }
    }
  }
</script>
<style lang="less" scoped>
  .search {
    & /deep/ .el-form-item__label {
      font-size: 1.8rem;
    }
  }

  .f-g-sd1 {
    position: relative;
    float: left;
    width: 95px;
    margin-right: -95px;
  }

  .f-g-mn1 {
    float: right;
    width: 100%;
  }

  .f-g-mn1c {
    margin-left: 100px;
  }

  .detail-body {
    color: rgba(250, 250, 250, 0.9);

    &::after {
      content: '';
      display: block;
      visibility: hidden;
      height: 0;
      clear: both;
    }
  }

  .item-inline {
    width: 50%;
    float: left;
    margin-bottom: 20px;

    .item-name {
      display: inline-block;
    }

    .item-context {
      display: inline-block;
    }
  }

  .no-data {
    text-align: center;
    font-size: 14px;

    .tips {
      padding: 20px 0px;
      color: #666;
    }
  }

  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }

  .footer {
    margin-top: 40px;
    color: #ffffff;
    font-size: 16px;

    .info {
      .select {
        width: 52px;
        height: 35px;
      }

      .select /deep/ .el-input__inner {
        padding-left: 5px;
        padding-right: 0;
      }

      .select /deep/ .el-input__icon {
        width: 15px;
      }
    }

    .paging {
      text-align: right;
      padding-right: 30px;
      margin-top: -20px;

      & /deep/ .el-pagination.is-background .btn-prev {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination.is-background .btn-next {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pager li {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination__total,
      & /deep/ .el-pagination__sizes {
        float: left;
        font-size: 1.6rem;
        height: 35px;
        line-height: 35px;
      }

      & /deep/ .el-input__inner {
        height: 35px;
      }

      & /deep/ .el-input .el-input__inner {
        padding-right: 20px;
        color: #FFFFFF;
        font-size: 1.6rem;
      }
    }
  }

  .content {
    margin-top: 10px;

    & /deep/ .el-checkbox__inner {
      height: 20px;
      width: 20px;
      font-size: 28px;
      font-weight: 400;
      color: rgba(255, 255, 255, 1);
      background: none;
    }

    & /deep/ .el-tooltip__popper {
      background-color: red;
    }

    & /deep/ .el-checkbox__inner::after {
      border-width: 2px;
      height: 6px;
      left: 6px;
      top: 3px;
      width: 4px;
    }

    & /deep/ .el-checkbox__input.is-indeterminate .el-checkbox__inner::before {
      top: 8px;
      background: white;
    }
  }

  .line {
    display: inline-flex;
    height: 21px;
    width: 1px;
    background: rgba(0, 255, 255, 1);
    margin: 0 10px;
    vertical-align: middle;
  }

  .dialogBtn {
    font-size: 1.6rem;

    &:hover {
      background: rgba(12, 161, 151, 1);
    }
  }

  /deep/ .el-transfer-panel {
    background: transparent;
  }
  /deep/ .el-tabs--border-card {
    background: #052938;
  }
</style>
<style lang="less">
  /**
   * 修改样式   覆盖默认的饿了么全局样式
   */
  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }
</style>
